package com.af.security.mgt.permisson;

import com.af.security.mgt.operation.Operation;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : zhenyun.su
 * @since : 2019/1/24
 * @Comment: 自定义全局异常
		id    菜单ID，也是权限ID
		pid   菜单父节点ID，根节点为 0
		text  菜单名称,
		i18n  多语言标识,
		permission  授权标识,
		heading true 标识菜单
		link  内部链接,
		externalLink  外部链接,
		target  链接打开的页面显示的目标,  _blank, _self, _parent, _top
		icon  菜单图标,
		badge  徽标数，展示的数字，默认红色
		shortcut 快捷菜单，
		shortcutRoot 快捷菜单根节点，
		sort   菜单排序,
		operation_ids  功能点id列表, 例如1,2,3,
		hide  true隐藏菜单
		disabled  true禁用菜单
		sort 菜单排序，在同一个pid下
		List<Permission> children 子菜单，正常支持3层 *
 		List<Operation> operations 根据operation_ids 存放操作权限, 例如：user_view, user_edit
 */
@Entity
@Table(name="sys_permission")
public class Permission implements Serializable{
	@Id
	@Column(name="permission_id")
	private Long id;
	@Column(name="parent_id")
	private Long pid;
	private String text;
	private Boolean heading;
	private String i18n;
	private String permission;
	private String link;
	private String externalLink;
	private String target;
	private String icon;
	private Integer badge;
	private Boolean shortcut;
	private Boolean shortcutRoot;
	private Integer sort ;
	private Boolean hide;
	private Boolean disabled ;
	private String operationIds;
	@Transient
    private List<Permission> children = new ArrayList<>();
	@Transient
	private List<Operation> operations = new ArrayList<>();

	public static Permission heading(Long pid, String text, String i18n, String permission,
								String icon, Integer badge, Integer sort) {
		return of(pid, text, true, i18n, permission,
				null, null, null, icon, badge, false, false,
				sort, false, null);
	}
	public static Permission menus(Long pid, String text, String i18n, String permission,
								String link, String externalLink, String target, String icon, Integer badge,
								Boolean shortcut, Boolean shortcutRoot, Integer sort, Boolean hide, String operationIds){
		return of(pid, text, false, i18n, permission,
				link, externalLink, target, icon, badge, shortcut, shortcutRoot,
				sort, hide, operationIds);
	}

	public static Permission of(Long pid, String text, Boolean heading, String i18n, String permission,
								String link, String externalLink, String target, String icon, Integer badge,
								Boolean shortcut, Boolean shortcutRoot, Integer sort, Boolean hide, String operationIds){
		Permission per = new Permission();
		per.setPid(pid);
		per.setText(text);
		per.setHeading(heading);
		per.setI18n(i18n);
		per.setPermission(permission);
		per.setLink(link);
		per.setExternalLink(externalLink);
		per.setTarget(target);
		per.setIcon(icon);
		per.setBadge(badge);
		per.setShortcut(shortcut);
		per.setShortcutRoot(shortcutRoot);
		per.setSort(sort);
		per.setHide(hide);
		per.setDisabled(false);
		per.setOperationIds(operationIds);
		return per;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Boolean getHeading() {
		return heading;
	}

	public void setHeading(Boolean heading) {
		this.heading = heading;
	}

	public String getI18n() {
		return i18n;
	}

	public void setI18n(String i18n) {
		this.i18n = i18n;
	}

	public String getPermission() {
		return permission;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public String getExternalLink() {
		return externalLink;
	}

	public void setExternalLink(String externalLink) {
		this.externalLink = externalLink;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public Integer getBadge() {
		return badge;
	}

	public void setBadge(Integer badge) {
		this.badge = badge;
	}

	public Boolean getShortcut() {
		return shortcut;
	}

	public void setShortcut(Boolean shortcut) {
		this.shortcut = shortcut;
	}

	public Boolean getShortcutRoot() {
		return shortcutRoot;
	}

	public void setShortcutRoot(Boolean shortcutRoot) {
		this.shortcutRoot = shortcutRoot;
	}

	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	public Boolean getHide() {
		return hide;
	}

	public void setHide(Boolean hide) {
		this.hide = hide;
	}

	public Boolean getDisabled() {
		return disabled;
	}

	public void setDisabled(Boolean disabled) {
		this.disabled = disabled;
	}

	public String getOperationIds() {
		return operationIds;
	}

	public void setOperationIds(String operationIds) {
		this.operationIds = operationIds;
	}

	public List<Permission> getChildren() {
		return children;
	}

	public void setChildren(List<Permission> children) {
		this.children = children;
	}

	public List<Operation> getOperations() {
		return operations;
	}

	public void setOperations(List<Operation> operations) {
		this.operations = operations;
	}

	@Override
	public String toString() {
		return "Permission{" +
				"id=" + id +
				", pid=" + pid +
				", text='" + text + '\'' +
				", heading=" + heading +
				", i18n='" + i18n + '\'' +
				", permission='" + permission + '\'' +
				", link='" + link + '\'' +
				", externalLink='" + externalLink + '\'' +
				", target='" + target + '\'' +
				", icon='" + icon + '\'' +
				", badge=" + badge +
				", shortcut=" + shortcut +
				", shortcutRoot=" + shortcutRoot +
				", sort=" + sort +
				", hide=" + hide +
				", disabled=" + disabled +
				", operationIds='" + operationIds + '\'' +
				", children=" + children +
				", operations=" + operations +
				'}';
	}
}
